package com.java.androidmarket.service.impl;

import com.alibaba.fastjson.JSON;
import com.java.androidmarket.dao.UserDao;
import com.java.androidmarket.entity.User;
import com.java.androidmarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * description:
 * author:张腾
 * date:2021-04-24
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public String login(String userName,String userPwd) {

        User userDB = userDao.login(userName);
        if (userDB!=null){
            if (userPwd.equals(userDB.getUserPwd())){
                return JSON.toJSONString(userDB);
            }else {
                return "密码错误！";
            }
        }else {
            return "该账号不存在！";
        }
    }

    @Override
    public String getUserName(int userId) {
        return userDao.getUserName(userId);
    }

    @Override
    public User getUser(int userId) {
        User userDB = userDao.getUser(userId);
        return userDB;
    }

    public void uploadHead(String fileName,int userId){
        userDao.uploadHead(userId, fileName);
    }

    @Override
    public String register(String userName, String userPwd) {
        if (userDao.ifExistName(userName)>0) {
            return "用户名已存在";
        }else{
            userDao.register(userName,userPwd);
            User user = userDao.login(userName);
            return JSON.toJSONString(user);
        }
    }

    @Override
    public boolean checkName(String userName) {
        if (userDao.ifExistName(userName)>0) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updateUser(User user) {

        if (!checkName(user.getUserName())) {
            userDao.updateUser(user);
            return true;
        }else{
            return false;
        }
    }


}
